/**
 * Copyright (c) 2005-2012 springside.org.cn
 */
package com.itic.appbase.framework.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;

import com.itic.appbase.framework.plugins.logger.Log;

/**
 * 封装各种格式的编码解码工具类. 1.Commons-Codec的 hex/base64 编码 2.Commons-Lang的xml/html escape
 * 3.JDK提供的URLEncoder
 */
public class EncodingHelper {

    private static final String DEFAULT_URL_ENCODING = "UTF-8";
    private static final Logger LOGGER = Log.get(EncodingHelper.class);
    private static final String DECODE = "DecoderException";
    private static final String ENCODE = "EncoderException";

    private EncodingHelper() {
        super();
    }

    /**
     * Hex编码.
     * 
     * @param input
     *            需要转换的字节数组
     * @return String HEX串
     */
    public static String encodeHex(byte[] input) {
        return new String(Hex.encodeHex(input));
    }

    /**
     * Hex解码.
     * 
     * @param input
     *            需要转换的HEX串
     * @return byte 字节数组
     */
    public static byte[] decodeHex(String input) {
        try {
            return Hex.decodeHex(input.toCharArray());
        } catch (DecoderException e) {
            LOGGER.info(DECODE, e);
        }
        return new byte[0];
    }

    /**
     * Base64编码.
     * 
     * @param input
     *            需要转换的字节数组
     * @return String BASE64串
     */
    public static String encodeBase64(byte[] input) {
        return new String(Base64.encodeBase64(input));
    }

    /**
     * 使用UTF-8对字符串进行Base64编码.
     * 
     * @param input
     *            需要转换的字符串
     * @return String BASE64串
     */
    public static String encodeBase64(String input) {
        try {
            return new String(Base64.encodeBase64(input.getBytes(DEFAULT_URL_ENCODING)));
        } catch (UnsupportedEncodingException e) {
            LOGGER.info(ENCODE, e);
        }
        return "";
    }

    /**
     * Base64解码.
     * 
     * @param input
     *            需要解码的字符串
     * @return byte[] 字节数组
     */
    public static byte[] decodeBase64(String input) {
        return Base64.decodeBase64(input.getBytes());
    }

    /**
     * 使用UTF-8对字符串进行Base64解码.
     * 
     * @param input
     *            需要解码的字符串
     * @return String 字符串
     */
    public static String decodeBase64String(String input) {
        try {
            return new String(Base64.decodeBase64(input.getBytes()), DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            LOGGER.info(ENCODE, e);
        }
        return "";
    }

    /**
     * HTML转码. <br>
     * 如"bread" & "butter" ->&quot;bread&quot; &amp; &quot;butter&quot;.
     * 
     * @param html
     *            HTML字符串
     * @return String
     */
    public static String escapeHtml(String html) {
        return StringEscapeUtils.escapeHtml4(html);
    }

    /**
     * Html 解码.
     * 
     * @param htmlEscaped
     *            使用过转码的字符串
     * @return String
     */
    public static String unescapeHtml(String htmlEscaped) {
        return StringEscapeUtils.unescapeHtml4(htmlEscaped);
    }

    /**
     * XML转码.
     * 
     * @param xml
     *            需要转码的XML
     * @return String
     */
    public static String escapeXml(String xml) {
        return StringEscapeUtils.escapeXml10(xml);
    }

    /**
     * XML解码.
     * 
     * @param xmlEscaped
     *            使用过转码的XML
     * @return String
     */
    public static String unescapeXml(String xmlEscaped) {
        return StringEscapeUtils.unescapeXml(xmlEscaped);
    }

    /**
     * URL编码，UTF-8.
     * 
     * @param part
     *            需要编码的URL
     * @return String
     */
    public static String urlEncode(String part) {
        try {
            return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            LOGGER.info(ENCODE, e);
        }
        return "";
    }

    /**
     * URL解码，UTF-8.
     * 
     * @param part
     *            经过转码的URL
     * @return String
     */
    public static String urlDecode(String part) {
        try {
            return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            LOGGER.info(ENCODE, e);
        }
        return "";
    }
}
